<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统登录</div>
            <el-form :model="loginForm" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="account">
                    <el-input v-model="loginForm.account" placeholder="请输入账号">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="请输入密码"
                        v-model="loginForm.password"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
                <p class="login-tips"><a href="javascript:;" @click="toRegister">没有账号？点我注册</a></p>
            </el-form>
        </div>
    </div>
</template>

<script>
import axios from 'axios';
import { mapState,mapMutations } from 'vuex';

export default {
    data: function() {
        return {
            loginForm: {
                account: 'admin',
                password: '12345',
            },
            rules: {
                account: [{ required: true, message: '请输入账号', trigger: 'blur' },
			 { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
			 ],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' },
			 { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
			 ],
            },
        };
    },
    methods: { 
    	...mapMutations(['changeLogin']),//通过子组件定义的方法传递参数，在…mapMutations引用
		toRegister(){
			this.$router.push('/register');
		},
        submitForm() {
        	//this.interceptor()  //拦截请求        	
        	let that=this; //改变this指向
            that.$refs.login.validate(valid => {
                if (valid) {    
//            	let formData=new FormData()
// 					//把json数据转换为FormData格式
//					formData.append("userName",that.loginForm.userName)
//					formData.append("password",that.loginForm.password)
//					console.log(that.loginForm)					
            		axios({
            			method:'post',
            			//baseURL: 'http://localhost:8081',
            			//url:'/api/user/login', //不带token的验证
            			url:'/api/login/login', //带token验证
            			//data:formData,  //提交form表单数据
            			data:that.loginForm  //提交json格式数据
            		}).then(res=>{//请求成功时
            			//判断json对象为空，Object.keys(res.data.message).length!=0
            			console.log(res)
            			if (res.status==200 && res.data.code==-1){ //返回对象不空时
            				that.$message.success('用户名称或密码错误，请重新登陆');
            			}else if(res.status==200 && res.data.code==-2){
            				that.$message.success('登录失败,密码错误');
            			} else if (res.status==200 && res.data.code==200){
            				that.$message.success('登录成功');                       	
          					// 将用户token保存到vuex中
          					that.userToken =res.data.data;
          					console.log(that.userToken);
          					that.changeLogin({ Authorization: that.userToken });
          					console.log("成功保存token，准备跳转路由")
                        	that.$router.push('/dashboard');//发送跳转路由，登陆页面
            			}else{//返回空对象时
            				console.log(res)
            				that.$message.error('用户名称或密码错误，请重新登陆');		
            			}            			
            		}).catch(err=>{
            			console.log(err)
            			that.$message.error('登陆失败！')
            		}) 
                } else {
                    that.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
	   
	   register(){
		   
	   }
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: black;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>